`<template>
  <div class="dragon-boat-race">
    <div class="game-container" ref="gameContainer">
      <div class="water-animation"></div>

      <!-- Score and Controls Panel -->
      <div class="control-panel">
        <div class="score">得分: {{ score }}</div>
        <div class="speed">速度: {{ speed.toFixed(1) }}</div>
        <button @click="startGame" v-if="!gameStarted" class="start-btn">
          开始比赛
        </button>
      </div>

      <!-- Player's Dragon Boat -->
      <div
        class="dragon-boat player-boat"
        :style="{ left: playerPosition.x + 'px', top: playerPosition.y + 'px' }"
        :class="{ rowing: isRowing }"
      >
        <div class="boat-body"></div>
        <div class="dragon-head"></div>
        <div class="paddles"></div>
      </div>

      <!-- AI Boats -->
      <div
        v-for="(boat, index) in aiBoats"
        :key="index"
        class="dragon-boat ai-boat"
        :style="{ left: boat.x + 'px', top: boat.y + 'px' }"
        :class="{ rowing: gameStarted }"
      >
        <div class="boat-body"></div>
        <div class="dragon-head"></div>
        <div class="paddles"></div>
      </div>
    </div>

    <!-- Game Over Modal -->
    <div v-if="gameOver" class="game-over-modal">
      <h2>比赛结束!</h2>
      <p>最终得分: {{ score }}</p>
      <p>{{ gameOverMessage }}</p>
      <button @click="resetGame" class="restart-btn">重新开始</button>
    </div>
  </div>
</template>

<script>
export default {
  name: "DragonBoatRace",
  data() {
    return {
      score: 0,
      speed: 1,
      gameStarted: false,
      gameOver: false,
      gameOverMessage: "",
      isRowing: false,
      playerPosition: {
        x: 50,
        y: 300,
      },
      aiBoats: [
        { x: 50, y: 100 },
        { x: 50, y: 200 },
        { x: 50, y: 400 },
      ],
      gameLoop: null,
      keyState: {
        ArrowUp: false,
        ArrowDown: false,
        ArrowLeft: false,
        ArrowRight: false,
        Space: false,
      },
    };
  },
  mounted() {
    window.addEventListener("keydown", this.handleKeyDown);
    window.addEventListener("keyup", this.handleKeyUp);
  },
  beforeUnmount() {
    window.removeEventListener("keydown", this.handleKeyDown);
    window.removeEventListener("keyup", this.handleKeyUp);
    this.stopGameLoop();
  },
  methods: {
    startGame() {
      this.gameStarted = true;
      this.startGameLoop();
    },
    stopGameLoop() {
      if (this.gameLoop) {
        cancelAnimationFrame(this.gameLoop);
      }
    },
    startGameLoop() {
      const gameLoop = () => {
        if (!this.gameOver) {
          this.updateGame();
          this.gameLoop = requestAnimationFrame(gameLoop);
        }
      };
      this.gameLoop = requestAnimationFrame(gameLoop);
    },
    handleKeyDown(event) {
      if (this.keyState.hasOwnProperty(event.code)) {
        this.keyState[event.code] = true;
        if (event.code === "Space") {
          this.isRowing = true;
          this.speed = Math.min(this.speed + 0.2, 5);
        }
      }
    },
    handleKeyUp(event) {
      if (this.keyState.hasOwnProperty(event.code)) {
        this.keyState[event.code] = false;
        if (event.code === "Space") {
          this.isRowing = false;
          this.speed = Math.max(this.speed - 0.1, 1);
        }
      }
    },
    updateGame() {
      if (!this.gameStarted) return;

      // Update player position
      if (this.keyState.ArrowUp) {
        this.playerPosition.y = Math.max(
          50,
          this.playerPosition.y - this.speed * 2
        );
      }
      if (this.keyState.ArrowDown) {
        this.playerPosition.y = Math.min(
          450,
          this.playerPosition.y + this.speed * 2
        );
      }
      if (this.keyState.ArrowRight) {
        this.playerPosition.x = Math.min(
          750,
          this.playerPosition.x + this.speed * 3
        );
      }
      if (this.keyState.ArrowLeft) {
        this.playerPosition.x = Math.max(0, this.playerPosition.x - this.speed);
      }

      // Update AI boats
      this.aiBoats.forEach((boat, index) => {
        boat.x += Math.random() * this.speed * 1.5;

        if (boat.x > 800) {
          this.handleAiBoatFinish(index);
        }
      });

      // Update score
      this.score = Math.floor(this.playerPosition.x / 10);

      // Check win/lose condition
      if (this.playerPosition.x >= 750) {
        this.gameOver = true;
        this.gameOverMessage = "恭喜你赢得了比赛！";
      }
    },
    handleAiBoatFinish(index) {
      this.gameOver = true;
      this.gameOverMessage = "很遗憾，对手先到达终点！";
    },
    resetGame() {
      this.score = 0;
      this.speed = 1;
      this.gameStarted = false;
      this.gameOver = false;
      this.gameOverMessage = "";
      this.isRowing = false;
      this.playerPosition = { x: 50, y: 300 };
      this.aiBoats = [
        { x: 50, y: 100 },
        { x: 50, y: 200 },
        { x: 50, y: 400 },
      ];
    },
  },
};
</script>

<style scoped>
.dragon-boat-race {
  position: relative;
  width: 100%;
  height: 600px;
  overflow: hidden;
  background: #87ceeb;
}

.game-container {
  position: relative;
  width: 800px;
  height: 500px;
  margin: 0 auto;
  border: 2px solid #1a1a1a;
  overflow: hidden;
}

.water-animation {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    0deg,
    rgba(0, 119, 190, 0.3) 0%,
    rgba(0, 119, 190, 0.1) 100%
  );
  animation: water 2s linear infinite;
}

.dragon-boat {
  position: absolute;
  width: 100px;
  height: 40px;
  transition: transform 0.1s ease;
}

.boat-body {
  position: absolute;
  width: 100%;
  height: 100%;
  background: #8b4513;
  border-radius: 10px;
}

.dragon-head {
  position: absolute;
  width: 30px;
  height: 20px;
  background: #daa520;
  right: -20px;
  top: 10px;
  clip-path: polygon(0 0, 100% 50%, 0 100%);
}

.paddles {
  position: absolute;
  width: 100%;
  height: 100%;
}

.rowing .paddles::before,
.rowing .paddles::after {
  content: "";
  position: absolute;
  width: 10px;
  height: 20px;
  background: #8b4513;
  animation: rowing 0.5s linear infinite;
}

.control-panel {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 10;
  background: rgba(255, 255, 255, 0.8);
  padding: 10px;
  border-radius: 5px;
}

.start-btn,
.restart-btn {
  padding: 10px 20px;
  background: #4caf50;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  margin-top: 10px;
}

.game-over-modal {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: white;
  padding: 20px;
  border-radius: 10px;
  text-align: center;
  z-index: 100;
}

@keyframes water {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 50px 50px;
  }
}

@keyframes rowing {
  0%,
  100% {
    transform: rotate(-45deg);
  }
  50% {
    transform: rotate(45deg);
  }
}

.player-boat {
  z-index: 2;
}

.ai-boat {
  z-index: 1;
}
</style>`